/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.quickorm.sample;

import com.quickorm.sample.entity.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.UUID;

/**
 *
 * @author aaa
 */
public class Menu2 {

    public static void execute(int dataRowCount) {
        Program.userDao.deleteUserByLoginName("testuser");
        JdbcNormalInsert(dataRowCount);
        Program.userDao.deleteUserByLoginName("testuser");
        QuickOrmNormalInsert(dataRowCount);
        Program.userDao.deleteUserByLoginName("testuser");
    }

    public static void QuickOrmNormalInsert(int dataRowCount) {
        User testUser = new User();
        testUser.setName("TestUser");
        testUser.setLoginName("testuser");
        testUser.setPassword("xxxxxxxxxxxx");

        long beginTime = System.currentTimeMillis();
        for (int i = 0; i <= dataRowCount - 1; i++) {
            testUser.setUserId(UUID.randomUUID().toString().replace("-", ""));
            Program.userDao.save(testUser);
        }
        long endTime = System.currentTimeMillis();

        
        System.out.println(String.format("QuickOrm NORMAL INSERT %s rows data used time:%s ms", dataRowCount, (endTime - beginTime)));
    }

    public static void JdbcNormalInsert(int dataRowCount) {
        User testUser = new User();
        testUser.setName("TestUser");
        testUser.setLoginName("testuser");
        testUser.setPassword("xxxxxxxxxxxx");

        long beginTime = System.currentTimeMillis();
        try {
            Connection conn = Program.dataSource.getConnection();
            conn.setAutoCommit(false);

            for (int i = 0; i <= dataRowCount - 1; i++) {
                testUser.setUserId(UUID.randomUUID().toString().replace("-", ""));

                PreparedStatement stat = conn.prepareStatement("INSERT INTO User(UserId, Name, LoginName, Password) VALUES (?, ?, ?, ?);");
                stat.setObject(1, testUser.getUserId());
                stat.setObject(2, testUser.getName());
                stat.setObject(3, testUser.getLoginName());
                stat.setObject(4, testUser.getPassword());

                stat.executeUpdate();
            }
            conn.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        long endTime = System.currentTimeMillis();

        System.out.println(String.format("JDBC NORMAL INSERT %s rows data used time:%s ms", dataRowCount, (endTime - beginTime)));

    }
}
